package com.cn.springcloudwechat.controller.find.communication;

import com.cn.springcloudwechat.common.ajax.AjaxRes;
import com.cn.springcloudwechat.common.mybatis.Page;
import com.cn.springcloudwechat.common.utils.Base64ToFile;
import com.cn.springcloudwechat.common.utils.Const;
import com.cn.springcloudwechat.common.utils.ThreadLocalUtil;
import com.cn.springcloudwechat.controller.vaccine.log.LogInfo;
import com.cn.springcloudwechat.controller.vaccine.log.OperateType;
import com.cn.springcloudwechat.model.YmAnswer;
import com.cn.springcloudwechat.model.YmPraise;
import com.cn.springcloudwechat.model.YmUser;
import com.cn.springcloudwechat.model.attachment.YmAttachment;
import com.cn.springcloudwechat.model.attachment.YmAttachmentRelevance;
import com.cn.springcloudwechat.model.find.communication.YmAnswerVO;
import com.cn.springcloudwechat.model.find.communication.YmExchange;
import com.cn.springcloudwechat.model.find.communication.YmExchangeVO;
import com.cn.springcloudwechat.service.find.communication.CommunicationService;
import com.cn.springcloudwechat.service.personalcenter.service.YmUserPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/communication/")
@ResponseBody
public class communicationControllerAdmin {

    @Autowired
    CommunicationService communicationService;

    @Autowired
    YmUserPointService ymUserPointService;

    /**
     * 交流圈-发帖
     * @param ajaxRes
     * @param ymExchange
     * @return
     */
    @RequestMapping("insertExchange")
    @LogInfo(operateType = OperateType.INSERT, content = "交流圈-发帖")
    public String insertExchange(AjaxRes ajaxRes, YmExchange ymExchange){
        try {

            ymExchange.setUserId(ThreadLocalUtil.getUser().getUserId());
            Long exchangeId = communicationService.insertExchange(ymExchange);
            if (exchangeId != 0){
                ajaxRes.setSucceed(exchangeId, Const.OPERATE_SUCCEED);
            }else{
                ajaxRes.setSucceed(exchangeId, Const.OPERATE_SUCCEED);
            }
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }

        return ajaxRes.toJSONString();
    }

    /**
     * 交流圈-回复
     * @param ajaxRes
     * @param ymAnswer
     * @return
     */
    @RequestMapping("insertConsult")
    @LogInfo(operateType = OperateType.INSERT, content = "交流圈-回复")
    public String insertConsult(AjaxRes ajaxRes, YmAnswer ymAnswer){
        try {
            ymAnswer.setAnswerUserId(ThreadLocalUtil.getUser().getUserId());
            Integer integer = communicationService.insertAnser(ymAnswer,3);
            ajaxRes.setSucceed(integer, Const.OPERATE_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes.toJSONString();
    }

//    /**
//     * 交流圈-帖子列表
//     *
//     * @param ajaxRes
//     * @return
//     */
//    @RequestMapping("quertCommunicationList")
//    @LogInfo(operateType = OperateType.QUERY, content = "交流圈-帖子列表")
//    public String quertCommunicationList(AjaxRes ajaxRes,Integer type){
////        List<Map<String,Object>> list = new ArrayList<>();
//        try {
//            List<YmExchangeVO> ymExchanges = communicationService.quertYmExchangeList();
//            ajaxRes.setSucceed(ymExchanges, Const.OPERATE_SUCCEED);
//        }catch (Exception e){
//            ajaxRes.setFailMsg(e.getMessage());
//        }
//        return ajaxRes.toJSONString();
//    }

    /**
     * 点赞
     * @param ajaxRes
     * @param ymPraise
     * @return
     */
    @RequestMapping("addPraise")
    @LogInfo(operateType = OperateType.INSERT, content = "点赞")
    public String addPraise(AjaxRes ajaxRes,YmPraise ymPraise){
        try {
            ymPraise.setUserId(ThreadLocalUtil.getUser().getUserId());
            Integer integer = communicationService.praiseExist(ymPraise);
            String add="";
            if(integer == null || integer==0){
                Integer result = communicationService.todayPraiseCount(ymPraise);
                if(result == 0){
                    add="获得豆豆+5";
                    //todo
                    ymUserPointService.insertUserPoint(ymPraise.getUserId(),"点赞获得",null,"+5");
                }
            }
            if (integer!= null ||(integer!= null && !"".equals(integer)&& integer==0 )){
                //如果点过赞修改状态
                Integer update = communicationService.updatePraiseByUserIdAndTypeId(ymPraise);
                integer = communicationService.praiseExist(ymPraise);

                ajaxRes.setSucceed(integer, Const.OPERATE_SUCCEED);
            }else{
                //新增点赞
                communicationService.addPraise(ymPraise);
                integer = communicationService.praiseExist(ymPraise);
                if (!"".equals(add)){
                    ajaxRes.setSucceed(add, Const.OPERATE_SUCCEED);
                }else{
                    ajaxRes.setSucceed(integer, Const.OPERATE_SUCCEED);
                }

            }
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes.toJSONString();
    }


    /**
     * 交流圈-帖子详情
     * @param ajaxRes
     * @return
     */
    @RequestMapping("quertCommunicationDetail")
    @LogInfo(operateType = OperateType.QUERY, content = "交流圈-帖子详情")
    public String quertCommunicationDetail(AjaxRes ajaxRes,Long exchangeId,Long userId,Integer type){
        List<Map<String,Object>> list = new ArrayList<>();
        try {
                Map<String,Object> map = new HashMap<>();
                //帖子内容
                YmExchangeVO ymExchangeVO = communicationService.selectByPrimaryKey(exchangeId);
                //帖子回复内容
                List<YmAnswerVO> ymAnswers = communicationService.queryYmAnswerDetail(exchangeId,3);
                //点赞数
                YmPraise ymPraise = new YmPraise();
                ymPraise.setPraiseTypeId(exchangeId);
                ymPraise.setPraiseType("3");
                ymPraise.setUserId(userId == null?ThreadLocalUtil.getUser().getUserId():userId);
                Integer count = communicationService.praiseCount(ymPraise);
                Integer state=communicationService.praiseExist(ymPraise);
                //图片附件
                YmAttachmentRelevance ymAttachmentRelevance = new YmAttachmentRelevance();
                ymAttachmentRelevance.setRelevanceId(exchangeId);
                ymAttachmentRelevance.setRelevanceTpyeId("3");
                List<YmAttachment> ymAttachments = communicationService.selectByRelevanceIdAndType(ymAttachmentRelevance);
                map.put("exchange",ymExchangeVO);
                map.put("ymAnswers",ymAnswers);
                map.put("praise",count);
                map.put("answersCount",ymAnswers.size());
                map.put("ymAttachments",ymAttachments);
                map.put("praiseState",state);
                list.add(map);
            ajaxRes.setSucceed(list, Const.OPERATE_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes.toJSONString();
    }

    /**
     * 删除帖子
     * @param ajaxRes
     * @param exchangeId
     * @return
     *
     */
    @RequestMapping("deleteExchangeBychangeId")
    @LogInfo(operateType = OperateType.UPDATE, content = "删除帖子")
    public AjaxRes DeleteExchangeByExchangeId(AjaxRes ajaxRes, @RequestParam("exchangeId[]")String [] exchangeId,String state){
        try{
            Integer i=communicationService.deleteExchangeByExchangeId(exchangeId,state);
            ajaxRes.setSucceed(i,Const.DEL_SUCCEED);
        }catch(Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 删除回复
     * @param ajaxRes
     * @param answerId
     * @return
     */
    @RequestMapping("deleteAnswerByAnswerId")
    @LogInfo(operateType = OperateType.UPDATE, content = "删除回复")
    public  AjaxRes DeleteAnswerByAnswerId(AjaxRes ajaxRes,@RequestParam("answerId[]")String [] answerId){
        try{
            Integer i=communicationService.deleteAnswerByAnswerId(answerId);
            ajaxRes.setSucceed(i,Const.DEL_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }


    /**
     * 交流圈-帖子列表-管理后台
     *
     * @param ajaxRes
     * @return
     */
    @RequestMapping("quertCommunicationListMrg")
    @LogInfo(operateType = OperateType.QUERY, content = " 交流圈-帖子列表-管理后台")
    public String quertCommunicationListMrg(AjaxRes ajaxRes,YmExchangeVO ymExchangeVO,Page<YmExchangeVO> page){
        try {
            Page<YmExchangeVO> ymExchanges = communicationService.quertYmExchangeListMrg(ymExchangeVO, page);

            ajaxRes.setSucceed(ymExchanges, Const.OPERATE_SUCCEED);

        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes.toJSONString();
    }

    /**
     * 交流圈-回复-后台管理
     * @param ajaxRes
     * @return
     */
    @RequestMapping("quertCommunicationDetailMrg")
    @LogInfo(operateType = OperateType.QUERY, content = "交流圈-回复-后台管理")
    public String quertCommunicationDetailMrg(AjaxRes ajaxRes,YmExchangeVO ymExchangeVO,Page<YmAnswerVO> page){

        try {
            Page<YmAnswerVO> ymAnswerVOPage = communicationService.quertCommunicationDetailMrg(ymExchangeVO, page);

            ajaxRes.setSucceed(ymAnswerVOPage, Const.OPERATE_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes.toJSONString();
    }

    /**
     * 修改帖子排序标签
     * @param ajaxRes
     * @param exchangeId
     * @return
     */
    @RequestMapping("updateTypeOrder")
    @LogInfo(operateType = OperateType.UPDATE, content = "修改帖子排序标签")
    public AjaxRes updateTypeOrder(AjaxRes ajaxRes,@RequestParam("exchangeId[]")String [] exchangeId,@RequestParam("typeOrder[]")String [] typeOrder){
        try {
            Integer integer = communicationService.updateTypeOrder(exchangeId, typeOrder);
            ajaxRes.setSucceed(integer, Const.OPERATE_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 交流圈-详情-后台管理
     * @param ajaxRes
     * @return
     */
    @RequestMapping("quertExchangenDetailMrg")
    @LogInfo(operateType = OperateType.QUERY, content = "交流圈-详情-后台管理")
    public String quertExchangenDetailMrg(AjaxRes ajaxRes,YmExchangeVO ymExchangeVO){

        try {
            YmExchangeVO ymExchangeVO1 = communicationService.quertExchangenDetailMrg(ymExchangeVO);

            ajaxRes.setSucceed(ymExchangeVO1, Const.OPERATE_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes.toJSONString();
    }
}
